package com.ultimatech.wxservice.web.controller;

import com.ultimatech.webappdemo.authmgr.service.IAuthService;
import com.ultimatech.webappdemo.authmgr.vo.*;
import com.ultimatech.webappdemo.base.PageResult;
import com.ultimatech.webappdemo.base.model.SysGroup;
import com.ultimatech.webappdemo.base.model.SysMyPermission;
import com.ultimatech.webappdemo.base.model.SysRole;
import com.ultimatech.webappdemo.base.model.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by 张乐平 on 8/3 0003.
 */
@Controller
@RequestMapping("/system")
public class SystemController extends MainsiteErrorController {

    public IAuthService getAuthService() {
        return authService;
    }

    public void setAuthService(IAuthService authService) {
        this.authService = authService;
    }

    @Autowired
    private IAuthService authService;

    @RequestMapping("/authorization")
    public String authorization() {
        return "system/authorization";
    }

    @RequestMapping(value = "/authorization/users", method = RequestMethod.GET)
    public String users(UserVo vo, Model model) {
        PageResult<SysUser> result = this.getAuthService().getUserList(vo);
        List<SysRole> roles = this.getAuthService().getAllRoles();
        model.addAttribute("result", result);
        model.addAttribute("vo", vo);
        model.addAttribute("roles", roles);
        return "system/authorization/users";
    }

    @RequestMapping(value = "/authorization/users", method = RequestMethod.POST)
    public String addUser(AddUserVo vo, Model model) throws Exception {
        this.getAuthService().createUser(vo);
        return "redirect:/system/authorization/users";
    }

    @RequestMapping(value = "/authorization/users/update", method = RequestMethod.POST)
    public String updateUser(AddUserVo vo, Model model) throws Exception {
        this.getAuthService().modifyUser(vo);
        return "redirect:/system/authorization/users";
    }

    @RequestMapping(value = "/authorization/users/del/{userId}", method = RequestMethod.GET)
    public String delUser(@PathVariable Integer userId, Model model) throws Exception {
        this.getAuthService().delUser(userId);
        return "redirect:/system/authorization/users";
    }

    @RequestMapping(value = "/authorization/users/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public AddUserVo getUser(@PathVariable Integer userId, Model model) throws Exception {
        AddUserVo vo = this.getAuthService().getUserById(userId);
        return vo;
    }

    @RequestMapping(value="/authorization/roles/query",method = RequestMethod.GET)
    public String roles(RoleVo vo, Model model) {
        PageResult<SysRole> result = this.getAuthService().getRoleList(vo);
        model.addAttribute("result", result);
        model.addAttribute("vo", vo);
        return "system/authorization/roles";
    }

    @RequestMapping(value = "/authorization/roles/add", method = RequestMethod.POST)
    public String addRole(AddRoleVo vo, Model model) throws Exception {
        this.getAuthService().createRole(vo);
        return "redirect:/system/authorization/roles/query";
    }
    @RequestMapping(value = "/authorization/roles/query", method = RequestMethod.POST)
    @ResponseBody
    public AddRoleVo getRole(Integer roleId, Model model) throws Exception {
        AddRoleVo vo = this.getAuthService().getRoleById(roleId);
        return vo;
    }
    @RequestMapping(value = "/authorization/roles/update", method = RequestMethod.POST)
    public String updateRole(AddRoleVo vo, Model model) throws Exception {
        this.getAuthService().modifyRole(vo);
        return "redirect:/system/authorization/roles/query";
    }
    @RequestMapping(value = "/authorization/roles/del", method = RequestMethod.GET)
    public String delRole(Integer roleId, Model model) throws Exception {
        this.getAuthService().delRole(roleId);
        return "redirect:/system/authorization/roles/query";
    }
    @RequestMapping("/authorization/perms/query")
    public String perms(PermsVo vo, Model model) {
        PageResult<SysMyPermission> result = this.getAuthService().getpermList(vo);
        model.addAttribute("result", result);
        model.addAttribute("vo", vo);
        return "system/authorization/perms";
    }
    @RequestMapping(value = "/authorization/perms/query", method = RequestMethod.POST)
    @ResponseBody
    public PermsVo getperm(Integer roleId, Model model) throws Exception {
        PermsVo vo = this.getAuthService().getPermById(roleId);
        return vo;
    }
    @RequestMapping(value = "/authorization/perms/update", method = RequestMethod.POST)
    public String updatePerms(PermsVo vo, Model model) throws Exception {
        this.getAuthService().updatePerm(vo);
        return "redirect:/system/authorization/perms/query";
    }
    @RequestMapping(value = "/authorization/perms/del", method = RequestMethod.GET)
    public String delPerm(Integer roleId, Model model) throws Exception {
        this.getAuthService().delPerm(roleId);
        return "redirect:/system/authorization/perms/query";
    }

    @RequestMapping(value = "/authorization/groups", method = RequestMethod.GET)
    public String groups(GroupVo vo, Model model) {
        PageResult<SysGroup> result = this.getAuthService().getgroupList(vo);
        model.addAttribute("result", result);
        model.addAttribute("vo", vo);
        return "system/authorization/groups";
    }

    @RequestMapping(value = "/authorization/svrlist", method = RequestMethod.GET)
    public String svrList(GroupVo vo, Model model) {

        return "system/authorization/svrlist";
    }
}
